package com.pgfx.ejbModule.facade.impl;

import javax.ejb.Stateless;
import javax.naming.NamingException;

import com.pgfx.ejbModule.entidad.BaseEntity;
import com.pgfx.ejbModule.facade.BaseFacadeRemote;
import com.pgfx.ejbModule.utils.ServiceLocator;

@Stateless
public abstract class BaseFacadeRemoteBean<E> extends BaseFacadeBean<E>
		implements BaseFacadeRemote<E> {
	
	public static String suffix = "";
	public static String accessType = "remote";
	/**
	 * obtiene la instancia del beans cargado.
	 * @param c clase entidad correspondiente al beans deseado
	 * @return interfaz de manipulacion.
	 * @throws NamingException en caso de no encontrarse la instancia
	 */
	@SuppressWarnings("unchecked")
	public static BaseFacadeRemote getRemoteHome(Class<? extends BaseEntity> c) throws NamingException{
		//String prefix = Constant.EAR_FILE.getValue().equals("") ? "" : Constant.EAR_FILE.getValue() + "/";
		String prefix = suffix.equals("") ? "" : suffix + "/";
		String suffix =  "Facade" + (accessType.equals("") ? "" : "/" + accessType);
		
		String jndiName = prefix + c.getSimpleName() + suffix ;
		
		return (BaseFacadeRemote) ServiceLocator.getInstance().getContext().lookup(jndiName);
	}

	/**
	 * Obtiene el JNDI directamente del String pasado como parametro
	 * @param c clase entidad correspondiente al beans deseado
	 * @return interfaz de manipulacion.
	 * @throws NamingException en caso de no encontrarse la instancia
	 */
	@SuppressWarnings("unchecked")
	public static BaseFacadeRemote getRemoteHome(String jndiName) throws NamingException{
		//String prefix = Constant.EAR_FILE.getValue().equals("") ? "" : Constant.EAR_FILE.getValue() + "/";
		String prefix = suffix.equals("") ? "" : suffix + "/";
//		String suffix =  "Facade" + (accessType.equals("") ? "" : "/" + accessType);
		String suffix =  (accessType.equals("") ? "" : "/" + accessType);
		
		jndiName = prefix + jndiName + suffix;
		System.out.println("---------------------------------------" + jndiName);
		
		return (BaseFacadeRemote) ServiceLocator.getInstance().getContext().lookup(jndiName);
	}

}
